class MovieRentingSystem {
public:
    map<long long, int> SM2P; //shop|movie->price
    map<int, set<pair<int, int>>> UnrentedM2PS; //movie->(price, shop)
    set<tuple<int, int, int>> Rented; //price, shop, movie

    MovieRentingSystem(int n, vector<vector<int>>& entries) {
        for(auto &movie:entries)
        {
            int shop=movie[0], movieid=movie[1], price=movie[2];
            SM2P[1ll*shop<<32|movieid]=price;
            UnrentedM2PS[movieid].insert({price, shop});
        }
    }
    
    vector<int> search(int movie) {
        auto it=UnrentedM2PS.find(movie);
        if(it==UnrentedM2PS.end()) return {};
        vector<int> res;
        for(auto &[price, shop]:UnrentedM2PS[movie])
        {
            res.emplace_back(shop); 
            if(res.size()==5) break;
        }
        return res;
    }
    
    void rent(int shop, int movie) {
        int price=SM2P[1ll*shop<<32|movie];
        UnrentedM2PS[movie].erase({price, shop});
        Rented.insert({price, shop, movie});
    }
    
    void drop(int shop, int movie) {
        int price=SM2P[1ll*shop<<32|movie];
        Rented.erase({price, shop, movie});
        UnrentedM2PS[movie].insert({price, shop});
    }
    
    vector<vector<int>> report() {
        vector<vector<int>> res;
        for(auto &[price, shop, movie]:Rented)
        {
            res.push_back({shop, movie});
            if(res.size()==5) break;
        }
        return res;
    }
};

/**
 * Your MovieRentingSystem object will be instantiated and called as such:
 * MovieRentingSystem* obj = new MovieRentingSystem(n, entries);
 * vector<int> param_1 = obj->search(movie);
 * obj->rent(shop,movie);
 * obj->drop(shop,movie);
 * vector<vector<int>> param_4 = obj->report();
 */